<?php

namespace api\admin\controller;

use api\mall\model\StoreItemTagModel;
use api\mall\model\StoreTagMapModel;
use cmf\controller\RestAdminBaseController;

class TagController extends RestAdminBaseController
{
    public function index(){
        $booth_type = $this->request->get('type');
        $model = new StoreItemTagModel();
        $data = $model->where('tag_type',$booth_type)->whereOr('tag_type',0)->select();
        return $this->success('ok_i',$data);
    }

    public function save(){
        if($this->request->post()){
            $model = new StoreItemTagModel();
            $data = $this->request->post();
            if(empty($data['id'])){
                $model->save($data);
            }else{
                $model->where('id',$data['id'])->update($data);
            }
            return $this->success('ok');
        }else{
            return $this->fail('参数错误');
        }
    }

    public function delete(){
        if($this->request->route('id')){
            $id = $this->request->route('id');
            $model = new StoreItemTagModel();
            $model->where('id',$id)->delete();
            return $this->success('ok');
        }else{
            return $this->error('参数错误');
        }
    }

    public function select(){
        if($this->request->post()){
            $id = $this->request->post('id');
            $item_id = $this->request->post('item_id');
            if(empty($item_id)){
                $item_id = '000000';
            }
            $select = $this->request->post('add/b');
            $model = new StoreTagMapModel();
            if($select){
                $model->insert(['item_id' => $item_id,'tag_id' => $id]);
            }else{
                $model->where('tag_id',$id)->where('item_id',$item_id)->delete();
            }
            $this->success('ok select',$select);
        }
    }

}